package com.android.sunflower.ui.mygarden

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.*
import androidx.fragment.app.Fragment
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.sunflower.R
import com.android.sunflower.databinding.FragmentMyGardenBinding
import com.android.sunflower.logic.NotifyWaterService
import com.android.sunflower.ui.MainActivity
import java.util.*

class MyGardenFragment : Fragment() {

    private val navController by lazy { MainActivity.navController }


    inner class MyHandler:Handler(){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what){
                queryPlantedFSuccess->{
                    myGardenBinding.myPlantsList.adapter?.notifyDataSetChanged()
                    myGardenBinding.emptyText.visibility=View.GONE
                    myGardenBinding.myPlantsList.visibility=View.VISIBLE
                }
                refreshListByDelete->{
                    Toast.makeText(MainActivity.runningInstance,"删除成功",Toast.LENGTH_SHORT).show()
                    viewModel?.selectAll()
                }
                refreshListByUpdate->{
                    Toast.makeText(MainActivity.runningInstance,"浇水完成",Toast.LENGTH_SHORT).show()
                    viewModel?.selectAll()
                }
                queryPlantedFEmptyOrNull->{
                    myGardenBinding.emptyText.visibility=View.VISIBLE
                    myGardenBinding.myPlantsList.visibility=View.GONE
                    myGardenBinding.selectAllButton.hide()
                    myGardenBinding.selectNoneButton.hide()
                }
            }
        }
    }


    companion object{
        const val queryPlantedFSuccess=78
        const val refreshListByDelete=79
        const val refreshListByUpdate=80
        const val queryPlantedFEmptyOrNull=81
        val myGardenBinding get() = _myGardenBinding!!
        private var _myGardenBinding:FragmentMyGardenBinding?=null
        val viewModel get() = _viewModel
        private var _viewModel:MyGardenViewModel?=null
        val handler get() = _handler
        private var _handler:MyHandler?=null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _viewModel=ViewModelProvider(this).get(MyGardenViewModel::class.java)
        _handler=MyHandler()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _myGardenBinding=FragmentMyGardenBinding.inflate(inflater,container,false)


        return myGardenBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        myGardenBinding.myGardenToolbar.setNavigationOnClickListener {
            navController?.navigate(R.id.action_myGardenFragment_to_plantListFragment)
        }
        viewModel?.selectAll()
        //布局优化，因为所有的cell高度都是一样的
        myGardenBinding.myPlantsList.setHasFixedSize(true)
        myGardenBinding.myPlantsList.adapter=MyGardenListAdapter(viewModel?.plantedFList!!,
            viewModel?.plantedFInfoList!!)
        myGardenBinding.myPlantsList.layoutManager=LinearLayoutManager(MainActivity.runningInstance)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _myGardenBinding=null
    }

    override fun onDestroy() {
        super.onDestroy()
        _viewModel=null
    }
}